﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Cabal.Kernel.Provider;


namespace Cabal.Kernel
{
    public class BS
    {
        public BS(string bsvalue,string str2exp)
        {
            BSValue = bsvalue;
            Str2Exp = str2exp;
            GotTime = DateTime.Now;
            LastTime = GotTime;
        }
        public BS(string bsvalue, string str2exp,DateTime gottime)
        {
            BSValue = bsvalue;
            Str2Exp = str2exp;
            GotTime = gottime;
            LastTime = GotTime;
        }
        public BS(XmlNode xmlnode)
        {
            FormXmlNode(xmlnode);
        }

        public static bool BS_AlwaysUseable = false;

        public string BSValue;
        public string Str2Exp;
        public string VerifyCode;
        public DateTime GotTime;
        public DateTime LastTime;
        public int Leaving;

        bool automove = true;
        public BSControl Parent;
        public void Used()
        {
            Leaving--;
            if (Leaving < 0)
                Leaving = 0;

            if (Leaving == 0 && Parent != null && automove)
                Parent.MoveBS(this, IsVerifyBS());
        }

        public bool UseableVerify
        {
            get
            {
                if (BS_AlwaysUseable)
                    return true;

                if (GotTime.Day == DateTime.Now.Day && Leaving > 0)
                    return true;
                else
                    return false;
            }
        }
        public bool UseableNomal
        {
            get
            {
                if (BS_AlwaysUseable)
                    return true;

                if (LastTime.Day < DateTime.Now.Day || Leaving > 0)
                    return true;
                else
                    return false;
            }
        }
   /**/ public string CalcStr2()
        {
            throw new NotImplementedException();
        }
        public bool IsVerifyBS()
        {
            if (VerifyCode != null && VerifyCode.Length >= 4)
                return true;
            else
                return false;
        }

        public XmlNode ToXmlNode(XmlNode node)
        {
            XmlDocument doc = node.OwnerDocument;

            addElement(doc, node, "BSValue", BSValue);
            addElement(doc, node, "Str2Exp", Str2Exp);
            addElement(doc, node, "VerifyCode", VerifyCode);
            addElement(doc, node, "GotTime", GotTime.ToString());
            addElement(doc, node, "LastTime", LastTime.ToString());
            addElement(doc, node, "Leaving", Leaving.ToString());

            return node;
        }
        void FormXmlNode(XmlNode bssource)
        {
            BSValue    =                bssource.SelectSingleNode("BSValue"   ).InnerText;
            Str2Exp    =                bssource.SelectSingleNode("Str2Exp"   ).InnerText;
            VerifyCode =                bssource.SelectSingleNode("VerifyCode").InnerText;
            GotTime    = DateTime.Parse(bssource.SelectSingleNode("GotTime"   ).InnerText);
            LastTime   = DateTime.Parse(bssource.SelectSingleNode("LastTime"  ).InnerText);
            Leaving    =      int.Parse(bssource.SelectSingleNode("Leaving"   ).InnerText);
        }
        void addElement(XmlDocument doc, XmlNode node, string key, string value)
        {
            XmlElement element = doc.CreateElement(key);
            element.InnerText = value;
            node.AppendChild(element);
        }

        /*
         * 
         * <CabalSoul>
         *   <BSCollection>
         *     <FileInfo>
         *       <Version>1</Version>
         *     </FileInfo>
         *     
         *     <UseableBS>
         *       <Verify>
        |*         <BS>
        |*           <BSValue>31303132333435...FF9B</BSValue>
        |*           <Str2Exp>(((((((((0xfff...103)</Str2Exp>
        |*           <VerifyCode>AFE5</VerifyCode>
        |*           <GotTime>2008-8-8 20:08</GotTime>
        |*           <LastTime>2008-8-8 23:59</LastTime>
        |*           <Leaving>1</Leaving>
        |*         </BS>
         *       <Nomal />
         *     </UseableBS>
         *     <UsedBS>
         *       <Verify />
         *       <Nomal />
         *     </UsedBS>
         *
         */
    }

    public class User
    {
        public User(string username, string passowrd)
        {
            Username = username;
            Password = passowrd;
        }
        public User(string username, string passowrd, string bduss,DateTime logintime)
        {
            Username = username;
            Password = passowrd;
            Bduss = bduss;
            LoginTime = logintime;
        }
        public User(string bduss,DateTime logintime)
        {
            throw new NotImplementedException();
        }
        public User(XmlNode source)
        {
            FromXmlNode(source);
        }


        public string Username;
        public string Password;
        public string Bduss;
        public DateTime LoginTime;
        public bool NeedVerify = true;
        public bool BannedLogin = false;
        public bool BannedPost = false;
        public bool BannedMsg = false;
        public bool BannedSpace = false;
        public bool UseableSpace = false;
        public string Tag = String.Empty;

        bool autosort = true;
        public UserControl Parent;
        DateTime _lastopt = DateTime.Now; //上次时间
        DateTime _opttime = DateTime.Now; //当前时间
        public DateTime OperatorTime
        {
            get { return _opttime; }
            set
            {
                _opttime = value;
                SetLastTime();
                if (Parent != null && autosort)
                {
                    Parent.SortUser(this, NeedVerify);
                }
            }
        }
        public void SetLastTime()
        {
            _lastopt = _opttime;
        }
        public void RestoreTime()
        {
            OperatorTime = _lastopt;
        }
        public void ResetOptTime()
        {
            OperatorTime = DateTime.Now;
        }
        public void ResetOptTime(bool sort)
        {
            if(sort)
                OperatorTime = DateTime.Now;
            else
                _opttime = DateTime.Now;
        }
        public void ResetOptTime(UserControl uc, List<User> list)
        {
            _opttime = DateTime.Now;
            uc.SortUser(this, list);
        }

        public void Used()
        {
            ResetOptTime();
        }
        public void NotUse()
        {
            RestoreTime();
        }


        public XmlNode ToXmlNode(XmlNode node)
        {
            XmlDocument doc = node.OwnerDocument;

            EnvSupport.addElement(doc, node, "Username", Username);
            EnvSupport.addElement(doc, node, "Password", Password);
            EnvSupport.addElement(doc, node, "Bduss", Bduss);
            EnvSupport.addElement(doc, node, "LoginTime", LoginTime.ToString());
            EnvSupport.addElement(doc, node, "NeedVerify", NeedVerify.ToString());
            EnvSupport.addElement(doc, node, "BannedLogin", BannedLogin.ToString());
            EnvSupport.addElement(doc, node, "BannedPost", BannedPost.ToString());
            EnvSupport.addElement(doc, node, "BannedMsg", BannedMsg.ToString());
            EnvSupport.addElement(doc, node, "BannedSpace", BannedSpace.ToString());
            EnvSupport.addElement(doc, node, "UseableSpace", UseableSpace.ToString());
            EnvSupport.addElement(doc, node, "Tag", Tag);

            return node;
        }
        void FromXmlNode(XmlNode usersource)
        {
            Username     =                usersource.SelectSingleNode("Username").InnerText;
            Password     =                usersource.SelectSingleNode("Password").InnerText;
            Bduss        =                usersource.SelectSingleNode("Bduss").InnerText;
            LoginTime    = DateTime.Parse(usersource.SelectSingleNode("LoginTime").InnerText);
            NeedVerify   =     bool.Parse(usersource.SelectSingleNode("NeedVerify").InnerText);
            BannedLogin  =     bool.Parse(usersource.SelectSingleNode("BannedLogin").InnerText);
            BannedPost   =     bool.Parse(usersource.SelectSingleNode("BannedPost").InnerText);
            BannedMsg    =     bool.Parse(usersource.SelectSingleNode("BannedMsg").InnerText);
            BannedSpace  =     bool.Parse(usersource.SelectSingleNode("BannedSpace").InnerText);
            UseableSpace =     bool.Parse(usersource.SelectSingleNode("UseableSpace").InnerText);
            Tag          =                usersource.SelectSingleNode("Tag").InnerText;
        }
    }
}
